const fs = require('fs');
const path = require('path');

/*   html文件管理   */
function GetHtmlFiles(){
	let files = fs.readdirSync('./data/exampleDir');
	return files;
}
//获取exampleDir下的.html文件
exports.getHtmlDir = async ctx=>{
	let files = GetHtmlFiles();
	
	ctx.body = {
		data:files
	}
};
//向exampleDir中添加.html文件
exports.addHtmlFile = async ctx=>{
	let {data,name} = ctx.request.body;
	data = Buffer.from(data,'base64');
	let files = GetHtmlFiles();
	fs.writeFileSync(`./data/exampleDir/${name}`,data);
	ctx.body = {
		status:true
	}
};

/*
	js文件(jsDir)、模型文件(modelDir)管理
	需要选择或创建目录保存文件
*/
function GetJsDir(basePath){
	let dir = [];
	let result = fs.readdirSync(basePath);
	let len = result.length;
	for(let i = 0;i < len ;i++){
		let newPath = path.join(basePath,result[i]);
		if(fs.lstatSync(newPath).isDirectory()){
			let children = GetJsDir(newPath);
			dir.push({
				parent:basePath,
				type:'dir',
				path:newPath.replace('data\\jsDir\\threeJs\\',''),
				children
			});
		}else{
			dir.push({
				parent:basePath,
				type:'file',
				path:newPath.replace('data\\jsDir\\threeJs\\','')
			})
		}
	}
	return dir;
}
exports.getJsDir = async ctx=>{
	let dir = GetJsDir('./data/jsDir/threeJs');
	ctx.body = {
		data:dir
	}
};